home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Programming / C Reference Card 3.0 / C Reference Card / C Reference Card.rsrc / TEXT_403_3.txt < prev    next >
Encoding:
Text File  |  1997-07-17  |  6.5 KB  |  129 lines

  1. OPERATORS : operator precedence table, using masks
  2. _________________________________________________________________________
  3.  
  4.  
  5.                        OPERATOR PRECEDENCE TABLE
  6.     =================================================================
  7.    | ::        scope resolution          class_name::member          |
  8.    | ::        #global                   ::name                      |
  9.    |-----------------------------------------------------------------|
  10.    | .         member selection          object.member               |
  11.    | ->        member selection          pointer->member             |
  12.    | []        subscripting              pointer[ expr ]             |
  13.    | ()        function call             expr( expr_list )           |
  14.    | ++        post increment            lvalue++                    |
  15.    | --        post decrement            lvalue--                    |
  16.    |-----------------------------------------------------------------|
  17.    | sizeof    #size of object           sizeof expr                 |
  18.    | sizeof    #size of type             sizeof( type )              |
  19.    | ++        #pre increment            ++lvalue                    |
  20.    | --        #pre decrement            --lvalue                    |
  21.    | ~         #complement               ~expr (bitwise negation)    |
  22.    | !         #not                      !expr                       |
  23.    | -         #unary minus              -expr                       |
  24.    | +         #unary plus               +expr                       |
  25.    | &         #address of               &lvalue                     |
  26.    | *         #dereference              *expr  (value at address)   |
  27.    | new       create (allocate)         new type                    |
  28.    | delete    destroy (de-allocate)     delete pointer              |
  29.    | delete[]  destroy array             delete[] pointer            |
  30.    | ()        #cast (type conversion)   ( type )expr                |
  31.    |-----------------------------------------------------------------|
  32.    | .*        member selection          object.*pointer-to-member   |
  33.    | ->*       member selection          pointer->*pointer-to-member |
  34.    |-----------------------------------------------------------------|
  35.    | *         multiply                  expr * expr                 |
  36.    | /         divide                    expr / expr                 |
  37.    | %         modulo (remainder)        expr % expr                 |
  38.    |-----------------------------------------------------------------|
  39.    | +         add (plus)                expr + expr                 |
  40.    | -         subtract (minus)          expr - expr                 |
  41.    |-----------------------------------------------------------------|
  42.    | <<        shift left                expr << expr                |
  43.    | >>        shift right               expr >> expr                |
  44.    |-----------------------------------------------------------------|
  45.    | <         less than                 expr < expr                 |
  46.    | <=        less than or equal to     expr <= expr                |
  47.    | >         greater than              expr > expr                 |
  48.    | >=        greater than or equal to  expr >= expr                |
  49.    |-----------------------------------------------------------------|
  50.    | ==        equal to                  expr == expr                |
  51.    | !=        not equal to              expr != expr                |
  52.    |-----------------------------------------------------------------|
  53.    | &         bitwise AND               expr & expr                 |
  54.    |-----------------------------------------------------------------|
  55.    | ^         bitwise exclusive OR      expr ^ expr                 |
  56.    |                                     one or the other,           |
  57.    |                                     but not both                |
  58.    |-----------------------------------------------------------------|
  59.    | |         bitwise inclusive OR      expr | expr                 |
  60.    |-----------------------------------------------------------------|
  61.    | &&        logical AND               expr && expr                |
  62.    |-----------------------------------------------------------------|
  63.    | ||        logical inclusive OR      expr || expr                |
  64.    |-----------------------------------------------------------------|
  65.    | ? :       #conditional expression   expr ? expr : expr          |
  66.    |-----------------------------------------------------------------|
  67.    | =         #simple assignment        expr = expr                 |
  68.    | *=        #multiply and assign      expr *= expr                |
  69.    | /=        #divide and assign        expr /= expr                |
  70.    | %=        #modulo and assign        expr %= expr                |
  71.    | +=        #add and assign           expr += expr                |
  72.    | -=        #subtract and assign      expr -= expr                |
  73.    | <<=       #shift left and assign    expr <<= expr               |
  74.    | >>=       #shift right and assign   expr >>= expr               |
  75.    | &=        #AND and assign           expr &= expr                |
  76.    | |=        #inclusive OR and assign  expr |= expr                |
  77.    | ^=        #exclusive OR and assign  expr ^= expr                |
  78.    |-----------------------------------------------------------------|
  79.    | throw     throw exception           throw expr                  |
  80.    |-----------------------------------------------------------------|
  81.    | ,         comma (sequencing)        expr, expr                  |
  82.     ================================================================= 
  83.     # Associativity is right to left (instead of left to right).
  84.  
  85.  
  86.  
  87. USING MASKS
  88.  
  89. You can use the bitwise AND and OR operators to determine whether certain bits are 'on' or 'off', or to turn certain bits 'on' or 'off'.  Here's an example:
  90.  
  91.   // bits.cp
  92.   #include <iostream.h>
  93.   
  94.   const short kBit1 = 01;
  95.   const short kBit2 = 02;
  96.   const short kBit3 = 04;
  97.   const short kBit4 = 010;
  98.   const short kBit5 = 020;
  99.   
  100.   void myCheckBits( short item );
  101.   
  102.   main()
  103.   {
  104.     short flags = 0;
  105.   
  106.     // turn bits on
  107.     flags = flags | kBit2;
  108.     flags = flags | kBit4;
  109.     
  110.     myCheckBits( flags );
  111.  
  112.     return 0;
  113.   }
  114.   
  115.   void myCheckBits( short item )
  116.   {
  117.     // check if bits are on
  118.     if( item & kBit1 )
  119.       cout << "Bit 1 is on." << endl;
  120.     if( item & kBit2 )
  121.       cout << "Bit 2 is on." << endl;
  122.     if( item & kBit3 )
  123.       cout << "Bit 3 is on." << endl;  
  124.     if( item & kBit4 )
  125.       cout << "Bit 4 is on." << endl;  
  126.     if( item & kBit5 )
  127.       cout << "Bit 5 is on." << endl;  
  128.   }
  129.   // end bits.cp